package mdl

import (
	//"fmt"
	"strings"

	//	"container/list"
	//	"fmt"
	"io/ioutil"
	"net/http"
	"strconv"

	//"database/sql"
	"github.com/axgle/mahonia"
	"hotime"
	//_ "github.com/mattn/go-sqlite3"
	"github.com/opesun/goquery"
	//"fmt"
)

//获取比赛
func GetBall(time int64, date string, isToday bool) []hotime.Map {

	db := hotime.Db
	where := hotime.Map{"AND": hotime.Map{"bdate": date, "type[#]": "0"}}
	var dest []hotime.Map
	if !isToday {

		//dest=db.Query("select * from ball where bdate=?", date)
		dest = db.Select("ball", "*", where)
	}

	if dest != nil && len(dest) != 0 {
		return dest
	}

	dest = make([]hotime.Map, 0)

	db.Delete("ball", where)

	var url = "http://live.500.com/?e=" + date
	if isToday {
		url = "http://live.500.com/2h1.php"
	}

	client := http.Client{}
	res, _ := client.Get(url)
	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)
	p, _ := goquery.ParseString(mahonia.NewDecoder("gbk").ConvertString(string(body)))
	trs := p.Find("#table_match")
	trs = trs.Find("tr")

	for i := 1; i < trs.Length(); i++ {
		ball := hotime.Map{}
		tr := trs.Eq(i)
		ball["type"] = "0"
		ball["time"] = time
		ball["bdate"] = date
		ball["id"] = tr.Attr("id")
		ball["order"], _ = strconv.Atoi(tr.Attr("order"))
		ball["status"], _ = strconv.Atoi(tr.Attr("status"))
		ball["gy"] = tr.Attr("gy")
		ball["yy"] = tr.Attr("yy")
		ball["lid"], _ = strconv.Atoi(tr.Attr("lid"))
		ball["fid"], _ = strconv.Atoi(tr.Attr("fid"))
		ball["sid"], _ = strconv.Atoi(tr.Attr("sid"))

		ball["infoid"], _ = strconv.Atoi(tr.Attr("infoid"))
		ball["r"], _ = strconv.Atoi(tr.Attr("r"))
		ball["living"], _ = strconv.Atoi(tr.Attr("living"))
		tds := tr.Find("td")
		ball["bnumber"] = tds.Eq(0).Text()
		ball["bgame"] = tds.Eq(1).Text()
		ball["blunci"] = tds.Eq(2).Text()
		ball["btime"] = tds.Eq(3).Text()
		ball["bstate"] = tds.Eq(4).Text()

		ball["bmasterteam"] = tds.Eq(5).Html()
		a := tds.Eq(5)
		a = a.Find("a")
		ball["bmasterhref"] = a.Attr("href")
		as := tds.Eq(6)
		as = as.Find("a")
		ball["bpoints"] = as.Eq(1).Text()
		if as.Eq(0).Text()==""{
			ball["bmoney"]="3"//未出结果
		}else{
			z,_:=hotime.ObjToInt(as.Eq(0).Text())
			k,_:=hotime.ObjToInt(as.Eq(2).Text())
			if z>k{
				ball["bmoney"]="1"//胜
			}
			if z==k{
				ball["bmoney"]="0"//平
			}
			if z<k{
				ball["bmoney"]="2"//输
			}
		}


		//ball["bmoney"] = `["` + as.Eq(0).Text() + `","` + as.Eq(2).Text() + `"]`
		ball["bslaveteam"] = tds.Eq(7).Html()
		a = tds.Eq(7)
		a = a.Find("a")
		ball["bslavehref"] = a.Attr("href")
		ball["bhalf"] = tds.Eq(8).Text()
		if isToday {
			ball["zhishul"] = tds.Eq(9).Text()
			ball["bet365"] = tds.Eq(10).Text()
			ball["zhishur"] = tds.Eq(11).Text()

			//ball["bmoney"] = ""
		} else {
			//ball["bmoney"] = tds.Eq(9).Html()

			ball["zhishul"] = ""
			ball["bet365"] = ""
			ball["zhishur"] = ""
		}

		videoNum := 10
		if isToday {
			videoNum = 12
		}
		a = tds.Eq(videoNum)
		a = a.Find("a")
		ball["bvideo"] = a.Attr("href")
		ball["bvideo"] = strings.Replace(ball["bvideo"].(string), "./", "http://live.500.com/", -1)
		as = tds.Eq(videoNum + 1)
		as = as.Find("a")
		ball["bfenxi"] = as.Eq(0).Attr("href")
		ball["bya"] = as.Eq(1).Attr("href")
		ball["bou"] = as.Eq(2).Attr("href")

		if !isToday {
			ball["id"] = db.Insert("ball", ball)
		}
		dest = append(dest, ball)
	}
	return dest
}
